package edu.nyu.hps.architecture;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JApplet;

public class DrawApplet extends JApplet implements ActionListener {
	DrawingPanel drawing_panel;

	public void init() {
		Container content_pane = getContentPane();
		setSize(700, 700);
		
		// Create an instance of DrawingPanel
		drawing_panel = new DrawingPanel();
		
		// Add the penel to the content pane.
		content_pane.add(drawing_panel);
		try {
			Socket client = new Socket("localhost", 4444);
			System.out.println("Connected");
			BufferedReader in = new BufferedReader(new InputStreamReader(client
					.getInputStream()));
			String inputLine;
//			drawing_panel.setLocations();
			while(true){
				while ((inputLine = in.readLine()) == null) {
					System.out.println("I am waiting!!");
					drawing_panel.setLocations();
				}
				
				System.out.println("Read from applet thread " + inputLine);
				String[] data = inputLine.split(":");
				int catx = Integer.parseInt(data[0].split(",")[0]);
				int caty = Integer.parseInt(data[0].split(",")[1]);
				int mousex = Integer.parseInt(data[1].split(",")[0]);
				int mousey = Integer.parseInt(data[1].split(",")[1]);
			drawing_panel.setLocations(catx, caty, mousex, mousey);
			long abc = System.currentTimeMillis();
			while(System.currentTimeMillis() - abc < 20000);
			}
		} catch (UnknownHostException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}

	}

	public void start() {
	}

	public void actionPerformed(ActionEvent e) {

		while (true) {

		}

	}

}
